const dayMap = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六', ]
var qqmapsdk;
//位置SDK
const QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    weekWeather: [],
    city: null
  },
  getFutureWeather: function(city) {
    var that = this
    wx.request({
      url: 'https://www.tianqiapi.com/api/?appid=64837846&appsecret=zuNpEpS7&version=v1',
      data: {
        city: that.data.city
      },
      success(res) {
        console.log("所有天气信息", res)
        let weekWeather = []
        if (res.statusCode === 200) {
          for (let i = 0; i < 7; i++) {
            let date = new Date()
            date.setDate(date.getDate() + i)
            weekWeather.push({
              day: dayMap[date.getDay()],
              date: `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`,
              temp1: res.data.data[i].tem1,
              temp2: res.data.data[i].tem2,
              weather: res.data.data[i].wea
            })
            weekWeather[0].day = "今天",
              that.setData({
                weekWeather: weekWeather
              })
          }
          console.log("未来天气信息", that.data.weekWeather)
        } else {
          console.log("数据请求失败")
        }
      }
    })
  },
  authorize() { //判断是否授权，已授权直接获取当前城市天气信息，没授权就提醒用户授权
    var that = this
    wx.getSetting({
      success: res => {
        if (!res.authSetting["scope.userLocation"]) {
          wx.authorize({
            scope: "scope.userLocation",
            success(res) {
              that.getCityAndWeather()
            },
            fail(err) {
              wx.showModal({
                title: '提示',
                content: '亲，打开位置权限才能获取实时天气信息哦',
                confirmText: "好的",
                confirmColor: "#4fa0e4",
                showCancel: false,
                success(res) {
                  wx.openSetting({
                    success(res) {
                      that.authorize()
                    }
                  })
                }
              })
            }
          })
        } else {
          that.getFutureWeather()
        }
      }
    })
  },
  getCityAndWeather() { //获取当前城市
    var that = this
    wx.getLocation({
      success: function(res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: res => {
            let city = res.result.address_component.city
            that.setData({
              city: city
            })
            that.getFutureWeather()
          }
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    this.authorize()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    setTimeout(function() {
      that.authorize()
      wx.stopPullDownRefresh()
    }, 1500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})